package com.alibaba.citrus.ots.sales.plan.facade.service.setting.contract;

import com.alibaba.citrus.ots.common.annotation.FacadeInvoker;
import com.alibaba.citrus.ots.common.lock.RedisLock;
import com.alibaba.citrus.ots.common.model.ImportRequest;
import com.alibaba.citrus.ots.sales.plan.api.setting.contract.ContractFileService;
import com.alibaba.citrus.ots.sales.plan.dto.api.request.contract.CompanyContractImportRequest;
import com.alibaba.citrus.ots.sales.plan.dto.api.request.contract.ContractImportRequest;
import com.alibaba.citrus.ots.sales.plan.facade.service.SalesPlanCommonService;
import com.alibaba.citrus.ots.sales.plan.facade.service.SalesPlanOutCommonService;
import com.alibaba.citrus.ots.sales.plan.functions.contract.ContractAbility;
import com.alibaba.citrus.ots.sales.plan.model.enums.ContractDataSourceEnum;
import com.alibaba.citrus.ots.sales.plan.model.enums.ContractStatDimensionEnum;
import com.alibaba.citrus.ots.sales.plan.sdo.ContractSDO;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.cz.base.tool.message.ErrorMessage;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.bcots.contants.BcOtsConstants;
import com.epoch.app.bcots.model.dto.EmployeeGetCurrentLoginResponse;
import com.epoch.app.bcots.model.dto.ScItemSDO;
import com.epoch.app.bcots.service.BcOtsService;
import com.epoch.app.otsplatformenhance.model.dto.CustomerAddressIndex;
import com.epoch.app.otsplatformenhance.opecustomeraddressindex.service.OpeCustomerAddressIndexService;
import com.epoch.app.otsplatformenhance.sdo.OrganizationSDO;
import com.epoch.app.otsplatformenhance.service.OtsPlatformEnhanceService;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

import javax.annotation.Resource;
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * @author luyi
 * @date 2021/11/1 4:25 下午
 */
@Component
public class ContractFileServiceImpl implements ContractFileService {

    private static final Log log = Log.getLogger(ContractFileServiceImpl.class);

    @Resource
    private ContractAbility contractAbility;

    @Resource
    private SalesPlanOutCommonService salesPlanOutCommonService;

    @Resource
    private OtsPlatformEnhanceService otsPlatformEnhanceService;

    @Resource
    private OpeCustomerAddressIndexService opeCustomerAddressIndexService;

    @Resource
    private BcOtsService bcOtsService;

    private static final String key = "ContractFileService.customerImportHandle";

    @FacadeInvoker
    @Override
    public Result<List<ContractImportRequest>> customerImportHandle(List<ContractImportRequest> requestList) {
        if (CollectionUtils.isEmpty(requestList)) {
            return Result.success(Lists.newArrayList());
        }
        Set<String> scItemSet = Sets.newHashSet();
        Set<String> customerAddressSet = Sets.newHashSet();
        Set<String> customerSet = Sets.newHashSet();
        Set<String> orgSalesOrganizationCodeSet = Sets.newHashSet();
        Set<String> channelCodeSet = Sets.newHashSet();
        Set<String> itemGroupSet = Sets.newHashSet();
        List<ContractImportRequest> requestList2 = Lists.newArrayList();
        EmployeeGetCurrentLoginResponse currentLoginResponse = bcOtsService.employeeGetCurrentLogin();

        for (ContractImportRequest importRequest : requestList) {
            if (StringUtils.isBlank(importRequest.getContractYear())) {
                importRequest.setErrorMessage(ErrorMessage.code("OTS-02-001-00-16-032", null).getDisplayErrorMessage());
                continue;
            }
            if (!Pattern.matches("^\\d{4}$", importRequest.getContractYear())) {
                importRequest.setErrorMessage(ErrorMessage.code("OTS-02-001-00-15-014", null).getDisplayErrorMessage());
                continue;
            }
            if (StringUtils.isBlank(importRequest.getScItemId())) {
                importRequest.setErrorMessage(ErrorMessage.code("OTS-02-001-00-16-026", null).getDisplayErrorMessage());
                continue;
            }
            if (StringUtils.isBlank(importRequest.getCustomerAddress())) {
                importRequest.setErrorMessage(ErrorMessage.code("OTS-02-001-00-16-027", null).getDisplayErrorMessage());
                continue;
            }
            if (StringUtils.isBlank(importRequest.getCustomerCode())) {
                importRequest.setErrorMessage(ErrorMessage.code("OTS-02-001-00-16-028", null).getDisplayErrorMessage());
                continue;
            }
            if (StringUtils.isBlank(importRequest.getOrgSalesOrganizationCode())) {
                importRequest.setErrorMessage(ErrorMessage.code("OTS-02-001-00-16-029", null).getDisplayErrorMessage());
                continue;
            }
            if (StringUtils.isBlank(importRequest.getChannelCode())) {
                importRequest.setErrorMessage(ErrorMessage.code("OTS-02-001-00-16-030", null).getDisplayErrorMessage());
                continue;
            }
            if (StringUtils.isBlank(importRequest.getItemGroup())){
                importRequest.setErrorMessage(ErrorMessage.code("OTS-02-001-00-16-031", null).getDisplayErrorMessage());
                continue;
            }
            scItemSet.add(importRequest.getScItemId());
            customerAddressSet.add(importRequest.getCustomerAddress());
            customerSet.add(importRequest.getCustomerCode());
            orgSalesOrganizationCodeSet.add(importRequest.getOrgSalesOrganizationCode());
            channelCodeSet.add(importRequest.getChannelCode());
            itemGroupSet.add(importRequest.getItemGroup());
            requestList2.add(importRequest);
        }
        if (CollectionUtils.isEmpty(requestList2)) {
            return Result.success(requestList);
        }

        if (!"103".equals(currentLoginResponse.getRelatedOrganizationType())) {
            log.error("ContractFileServiceImpl.customerImportHandle.orgSalesOrganizationCodeResult nothingness");
            ImportRequest.setRequestErrorMessage(requestList2, ErrorMessage.code("OTS-01-004-02-16-023", null).getDisplayErrorMessage());
            return Result.success(requestList);
        }

        //销售组织外部编码 置换 内部ID
        Map<String, OrganizationSDO> organizationMapByOutCode = salesPlanOutCommonService.getOrganizationMapByOutCode(orgSalesOrganizationCodeSet);
        if (organizationMapByOutCode == null) {
            log.error("ContractFileServiceImpl.customerImportHandle.orgSalesOrganizationCodeResult nothingness");
            ImportRequest.setRequestErrorMessage(requestList2, ErrorMessage.code("OTS-01-005-02-15-006", null).getDisplayErrorMessage());
            return Result.success(requestList);
        }


        //货品外部code 置换 内部ID
        Map<String, ScItemSDO> scItemMap = salesPlanOutCommonService.getScItemMapByOutId(scItemSet);
        if (scItemMap == null) {
            log.error("ContractFileServiceImpl.customerImportHandle.scItemResult nothingness");
            ImportRequest.setRequestErrorMessage(requestList2, ErrorMessage.code("OTS-01-005-02-15-001", null).getDisplayErrorMessage());
            return Result.success(requestList);
        }
        //查询 客户地址 (五键唯一)
        Set<String> organizationIdList = organizationMapByOutCode.values().stream().map(OrganizationSDO::getId).collect(Collectors.toSet());
        Map<String, CustomerAddressIndex> customerAddressMap = salesPlanOutCommonService.getCustomerAddressByFive(customerAddressSet, customerSet, organizationIdList, channelCodeSet, itemGroupSet);
        if (customerAddressMap == null){
            ImportRequest.setRequestErrorMessage(requestList2, ErrorMessage.code("OTS-01-004-02-15-002", null).getDisplayErrorMessage());
            return Result.success(requestList);
        }

        List<ContractSDO> request = Lists.newArrayList();
        for (ContractImportRequest importRequest : requestList2) {
            if (!Pattern.matches("^\\d{4}$", importRequest.getContractYear())) {
                importRequest.setErrorMessage(ErrorMessage.code("OTS-02-001-00-15-014", null).getDisplayErrorMessage());
                continue;
            }
            OrganizationSDO organization = organizationMapByOutCode.get(importRequest.getOrgSalesOrganizationCode());
            if (organization == null) {
                importRequest.setErrorMessage(ErrorMessage.code("OTS-01-005-02-15-006", null).getDisplayErrorMessage());
                continue;
            }

            //客户编码 + 客户地址编码 + 销售组织编码（ID） + 业务渠道编码 + 货品组
            CustomerAddressIndex customerAddress = customerAddressMap.get(importRequest.getCustomerCode() + importRequest.getCustomerAddress() + organization.getId() + importRequest.getChannelCode() + importRequest.getItemGroup());

            if (customerAddress == null) {
                importRequest.setErrorMessage(ErrorMessage.code("OTS-01-004-02-15-002", null).getDisplayErrorMessage());
                continue;
            }
            if (customerAddress.getStatus() == 0) {
                importRequest.setErrorMessage(ErrorMessage.code("OTS-01-004-02-15-007", null).getDisplayErrorMessage());
                continue;
            }

            if (!currentLoginResponse.getRelatedOrganizationId().equals(customerAddress.getOrgBussinessUnitCode())) {
                importRequest.setErrorMessage(ErrorMessage.code("OTS-01-004-02-15-013", null).getDisplayErrorMessage());
                continue;
            }
            ScItemSDO scItem = scItemMap.get(importRequest.getScItemId());
            if (Objects.isNull(scItem)) {
                importRequest.setErrorMessage(ErrorMessage.code("OTS-01-005-02-15-001", null).getDisplayErrorMessage());
                continue;
            }

            ContractSDO contract = new ContractSDO();
            BeanUtils.copyProperties(importRequest, contract);

            contract.setAnnualAmount(doubleToLong(importRequest.getAnnualAmount()));
            contract.setAnnualTaxIncludedBeforeTax(doubleToLong(importRequest.getAnnualTaxIncludedBeforeTax()));
            contract.setAprAmount(doubleToLong(importRequest.getAprAmount()));
            contract.setJanAmount(doubleToLong(importRequest.getJanAmount()));
            contract.setFebAmount(doubleToLong(importRequest.getFebAmount()));
            contract.setMarAmount(doubleToLong(importRequest.getMarAmount()));
            contract.setMayAmount(doubleToLong(importRequest.getMayAmount()));
            contract.setJuneAmount(doubleToLong(importRequest.getJuneAmount()));
            contract.setJulyAmount(doubleToLong(importRequest.getJulyAmount()));
            contract.setAugAmount(doubleToLong(importRequest.getAugAmount()));
            contract.setSeptAmount(doubleToLong(importRequest.getSeptAmount()));
            contract.setOctAmount(doubleToLong(importRequest.getOctAmount()));
            contract.setNovAmount(doubleToLong(importRequest.getNovAmount()));
            contract.setDecAmount(doubleToLong(importRequest.getDecAmount()));
            contract.setJanAfterDiscount(doubleToLong(importRequest.getJanAfterDiscount()));
            contract.setFebAfterDiscount(doubleToLong(importRequest.getFebAfterDiscount()));
            contract.setMarAfterDiscount(doubleToLong(importRequest.getMarAfterDiscount()));
            contract.setAprAfterDiscount(doubleToLong(importRequest.getAprAfterDiscount()));
            contract.setMayAfterDiscount(doubleToLong(importRequest.getMayAfterDiscount()));
            contract.setJuneAfterDiscount(doubleToLong(importRequest.getJuneAfterDiscount()));
            contract.setJulyAfterDiscount(doubleToLong(importRequest.getJulyAfterDiscount()));
            contract.setAugAfterDiscount(doubleToLong(importRequest.getAugAfterDiscount()));
            contract.setSeptAfterDiscount(doubleToLong(importRequest.getSeptAfterDiscount()));
            contract.setOctAfterDiscount(doubleToLong(importRequest.getOctAfterDiscount()));
            contract.setNovAfterDiscount(doubleToLong(importRequest.getNovAfterDiscount()));
            contract.setDecAfterDiscount(doubleToLong(importRequest.getDecAfterDiscount()));
            contract.setAnnualAfterDiscount(doubleToLong(importRequest.getAnnualAfterDiscount()));
            contract.setJanTaxIncludedBeforeTax(doubleToLong(importRequest.getJanTaxIncludedBeforeTax()));
            contract.setFebTaxIncludedBeforeTax(doubleToLong(importRequest.getFebTaxIncludedBeforeTax()));
            contract.setMarTaxIncludedBeforeTax(doubleToLong(importRequest.getMarTaxIncludedBeforeTax()));
            contract.setAprTaxIncludedBeforeTax(doubleToLong(importRequest.getAprTaxIncludedBeforeTax()));
            contract.setMayTaxIncludedBeforeTax(doubleToLong(importRequest.getMayTaxIncludedBeforeTax()));
            contract.setJuneTaxIncludedBeforeTax(doubleToLong(importRequest.getJuneTaxIncludedBeforeTax()));
            contract.setJulyTaxIncludedBeforeTax(doubleToLong(importRequest.getJulyTaxIncludedBeforeTax()));
            contract.setAugTaxIncludedBeforeTax(doubleToLong(importRequest.getAugTaxIncludedBeforeTax()));
            contract.setSeptTaxIncludedBeforeTax(doubleToLong(importRequest.getSeptTaxIncludedBeforeTax()));
            contract.setOctTaxIncludedBeforeTax(doubleToLong(importRequest.getOctTaxIncludedBeforeTax()));
            contract.setNovTaxIncludedBeforeTax(doubleToLong(importRequest.getNovTaxIncludedBeforeTax()));
            contract.setDecTaxIncludedBeforeTax(doubleToLong(importRequest.getDecTaxIncludedBeforeTax()));

            contract.setType(ContractStatDimensionEnum.CUSTOMER_SC_ITEM.getCode());
            contract.setDataSource(ContractDataSourceEnum.EXCEL.getCode());
            contract.setOrgBusinessUnitCode(customerAddress.getOrgBussinessUnitCode());
            contract.setOrgSalesOrganizationCode(customerAddress.getOrgSaleOrganizationCode());
            contract.setOrgSalesChannelCode(customerAddress.getOrgSaleChannelCode());
            contract.setOrgSalesDepartmentCode(customerAddress.getOrgSaleDepartmentCode());
            contract.setChannelCode(customerAddress.getChannelCode());
            contract.setCustomerCode(customerAddress.getCustomerCode());
            contract.setCustomerAddress(customerAddress.getAddressCode());
            contract.setManageEmployeeId(customerAddress.getPrincipal());
            contract.setCustomerAddressId(Objects.toString(customerAddress.getId(),null));

            contract.setScItemId(scItem.getScItemId());
            contract.setScItemCategoryId(scItem.getLeafCategoryId());

            request.add(contract);
        }

        //HashMap<String, ContractSDO> contractMap = new HashMap<>();

        if (CollectionUtils.isNotEmpty(request)) {

            Map<String, ContractSDO> contractMap = request.stream().collect(Collectors.toMap(contract -> SalesPlanCommonService.getCombinationKey(contract.getContractYear(), contract.getCustomerAddressId(), contract.getScItemId()), a -> a, (a, b) -> a));
            List<ContractSDO> contractSDOList = new ArrayList<>(contractMap.values());

            if (CollectionUtils.isNotEmpty(contractSDOList)){
                StopWatch stopWatch = new StopWatch("ContractFileService.customerImportHandle");
                stopWatch.start();
                boolean bool = true;
                while (bool) {
                    try (RedisLock lock = new RedisLock(BcOtsConstants.REDIS_CACHE_CODE(), key)) {
                        boolean locked = lock.lock();
                        if (!locked) {
                            log.info("ContractFileService.customerImportHandle.batchCreateContract:获取锁失败");
                            continue;
                        }
                        contractAbility.batchCreateContract(ContractStatDimensionEnum.CUSTOMER_SC_ITEM.getCode(), contractSDOList);
                        bool = false;
                    } catch (Exception e) {
                        log.info("ContractFileService.customerImportHandle.batchCreateContract={}", JSONObject.toJSONString(e));
                        break;
                    }
                }
                stopWatch.stop();
                log.info("ContractFileService.customerImportHandle.batchCreateContract:耗时" + stopWatch.getTotalTimeMillis() + "ms");
            }

        }
        return Result.success(requestList);
    }

    @Override
    @FacadeInvoker
    public Result<List<CompanyContractImportRequest>> companyImportHandle(List<CompanyContractImportRequest> requestList) {
        if (CollectionUtils.isEmpty(requestList)) {
            return Result.success(Lists.newArrayList());
        }
        EmployeeGetCurrentLoginResponse currentLoginResponse = bcOtsService.employeeGetCurrentLogin();
        if(!"103".equals(currentLoginResponse.getRelatedOrganizationType())){
            log.error("ContractFileServiceImpl.companyImportHandle.organizationMap no permissions");
            return Result.success(ImportRequest.setRequestErrorMessage(requestList, ErrorMessage.code("OTS-01-004-02-16-023", null).getDisplayErrorMessage()));
        }
        Set<String> organizationSet = Sets.newHashSet();
        Set<String> scItemSet = Sets.newHashSet();
        List<CompanyContractImportRequest> checkList = Lists.newArrayList();
        for (CompanyContractImportRequest importRequest : requestList) {
            //判空
            if (StringUtils.isBlank(importRequest.getContractYear())) {
                importRequest.setErrorMessage(ErrorMessage.code("OTS-02-001-00-16-032", null).getDisplayErrorMessage());
                continue;
            }
            if (StringUtils.isBlank(importRequest.getOrgBusinessUnitCode())) {
                importRequest.setErrorMessage(ErrorMessage.code("OTS-01-005-02-15-009", null).getDisplayErrorMessage());
                continue;
            }
            if (StringUtils.isBlank(importRequest.getScItemId())) {
                importRequest.setErrorMessage(ErrorMessage.code("OTS-01-005-02-15-003", null).getDisplayErrorMessage());
                continue;
            }
            if (!Pattern.matches("^\\d{4}$", importRequest.getContractYear())) {
                importRequest.setErrorMessage(ErrorMessage.code("OTS-02-001-00-15-014", null).getDisplayErrorMessage());
                continue;
            }
            organizationSet.add(importRequest.getOrgBusinessUnitCode());
            scItemSet.add(importRequest.getScItemId());
            checkList.add(importRequest);
        }
        if (CollectionUtils.isEmpty(checkList)){
            return Result.success(requestList);
        }
        Map<String, OrganizationSDO> organizationMap = salesPlanOutCommonService.getOrganizationMapByOutCode(organizationSet);
        if (organizationMap == null) {
            log.error("ContractFileServiceImpl.companyImportHandle.organizationMap nothingness");
            ImportRequest.setRequestErrorMessage(checkList, ErrorMessage.code("OTS-01-003-01-16-003", null).getDisplayErrorMessage());
            return Result.success(requestList);
        }
        Map<String, ScItemSDO> scItemMap = salesPlanOutCommonService.getScItemMapByOutId(scItemSet);
        if (scItemMap == null) {
            log.error("ContractFileServiceImpl.companyImportHandle.scItemMap nothingness");
            ImportRequest.setRequestErrorMessage(checkList, ErrorMessage.code("OTS-01-005-02-15-001", null).getDisplayErrorMessage());
            return Result.success(requestList);
        }
        Result<List<OrganizationSDO>> loginEmployeeBusinessUnit = otsPlatformEnhanceService.getLoginEmployeeBusinessUnits();
        OrganizationSDO unit = new OrganizationSDO();
        if (Objects.nonNull(loginEmployeeBusinessUnit) && CollectionUtils.isNotEmpty(loginEmployeeBusinessUnit.getResult())) {
            unit = loginEmployeeBusinessUnit.getResult().get(0);
        }

        List<ContractSDO> request = Lists.newArrayList();
        for (CompanyContractImportRequest importRequest : checkList) {
            OrganizationSDO organization = organizationMap.get(importRequest.getOrgBusinessUnitCode());
            if (Objects.isNull(organization)) {
                importRequest.setErrorMessage(ErrorMessage.code("OTS-01-003-01-16-003", null).getDisplayErrorMessage());
                continue;
            }
            if (organization.getType() != 103) {
                importRequest.setErrorMessage(ErrorMessage.code("OTS-01-003-01-15-011", null).getDisplayErrorMessage());
                continue;
            }
            if (!organization.getId().equals(unit.getId())) {
                importRequest.setErrorMessage(ErrorMessage.code("OTS-01-003-01-15-026", null).getDisplayErrorMessage());
                continue;
            }
            ScItemSDO scItem = scItemMap.get(importRequest.getScItemId());
            if (Objects.isNull(scItem)) {
                importRequest.setErrorMessage(ErrorMessage.code("OTS-01-005-02-15-001", null).getDisplayErrorMessage());
                continue;
            }
            ContractSDO contract = new ContractSDO();
            BeanUtils.copyProperties(importRequest, contract);

            contract.setAnnualAmount(doubleToLong(importRequest.getAnnualAmount()));
            contract.setAnnualTaxIncludedBeforeTax(doubleToLong(importRequest.getAnnualTaxIncludedBeforeTax()));
            contract.setAprAmount(doubleToLong(importRequest.getAprAmount()));
            contract.setJanAmount(doubleToLong(importRequest.getJanAmount()));
            contract.setFebAmount(doubleToLong(importRequest.getFebAmount()));
            contract.setMarAmount(doubleToLong(importRequest.getMarAmount()));
            contract.setMayAmount(doubleToLong(importRequest.getMayAmount()));
            contract.setJuneAmount(doubleToLong(importRequest.getJuneAmount()));
            contract.setJulyAmount(doubleToLong(importRequest.getJulyAmount()));
            contract.setAugAmount(doubleToLong(importRequest.getAugAmount()));
            contract.setSeptAmount(doubleToLong(importRequest.getSeptAmount()));
            contract.setOctAmount(doubleToLong(importRequest.getOctAmount()));
            contract.setNovAmount(doubleToLong(importRequest.getNovAmount()));
            contract.setDecAmount(doubleToLong(importRequest.getDecAmount()));
            contract.setJanAfterDiscount(doubleToLong(importRequest.getJanAfterDiscount()));
            contract.setFebAfterDiscount(doubleToLong(importRequest.getFebAfterDiscount()));
            contract.setMarAfterDiscount(doubleToLong(importRequest.getMarAfterDiscount()));
            contract.setAprAfterDiscount(doubleToLong(importRequest.getAprAfterDiscount()));
            contract.setMayAfterDiscount(doubleToLong(importRequest.getMayAfterDiscount()));
            contract.setJuneAfterDiscount(doubleToLong(importRequest.getJuneAfterDiscount()));
            contract.setJulyAfterDiscount(doubleToLong(importRequest.getJulyAfterDiscount()));
            contract.setAugAfterDiscount(doubleToLong(importRequest.getAugAfterDiscount()));
            contract.setSeptAfterDiscount(doubleToLong(importRequest.getSeptAfterDiscount()));
            contract.setOctAfterDiscount(doubleToLong(importRequest.getOctAfterDiscount()));
            contract.setNovAfterDiscount(doubleToLong(importRequest.getNovAfterDiscount()));
            contract.setDecAfterDiscount(doubleToLong(importRequest.getDecAfterDiscount()));
            contract.setAnnualAfterDiscount(doubleToLong(importRequest.getAnnualAfterDiscount()));
            contract.setJanTaxIncludedBeforeTax(doubleToLong(importRequest.getJanTaxIncludedBeforeTax()));
            contract.setFebTaxIncludedBeforeTax(doubleToLong(importRequest.getFebTaxIncludedBeforeTax()));
            contract.setMarTaxIncludedBeforeTax(doubleToLong(importRequest.getMarTaxIncludedBeforeTax()));
            contract.setAprTaxIncludedBeforeTax(doubleToLong(importRequest.getAprTaxIncludedBeforeTax()));
            contract.setMayTaxIncludedBeforeTax(doubleToLong(importRequest.getMayTaxIncludedBeforeTax()));
            contract.setJuneTaxIncludedBeforeTax(doubleToLong(importRequest.getJuneTaxIncludedBeforeTax()));
            contract.setJulyTaxIncludedBeforeTax(doubleToLong(importRequest.getJulyTaxIncludedBeforeTax()));
            contract.setAugTaxIncludedBeforeTax(doubleToLong(importRequest.getAugTaxIncludedBeforeTax()));
            contract.setSeptTaxIncludedBeforeTax(doubleToLong(importRequest.getSeptTaxIncludedBeforeTax()));
            contract.setOctTaxIncludedBeforeTax(doubleToLong(importRequest.getOctTaxIncludedBeforeTax()));
            contract.setNovTaxIncludedBeforeTax(doubleToLong(importRequest.getNovTaxIncludedBeforeTax()));
            contract.setDecTaxIncludedBeforeTax(doubleToLong(importRequest.getDecTaxIncludedBeforeTax()));

            contract.setType(ContractStatDimensionEnum.COMPANY_SC_ITEM.getCode());
            contract.setDataSource(ContractDataSourceEnum.CRM.getCode());
            contract.setOrgBusinessUnitCode(organization.getId());

            contract.setScItemId(scItem.getScItemId());
            contract.setScItemCategoryId(scItem.getLeafCategoryId());
            request.add(contract);
        }
        contractAbility.batchCreateContract(ContractStatDimensionEnum.COMPANY_SC_ITEM.getCode(), request);

        return Result.success(requestList);
    }

    private Long doubleToLong(Double d) {
        if (d == null) {
            return 0L;
        }
        return (long) (d * 100);
    }
}
